{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周回顾\n",
    "\n",
    "> *list一般方法/函数的所有使用场景及案例，方法如：\n",
    ">>> 1.append( )\n",
    ">>> 2.pop(index)/remove( )\n",
    ">>> 3.count( )\n",
    ">>> 4.index( )\n",
    ">>> 5.extend( )\n",
    ">>> 6.sort( )\n",
    ">>> .....\n",
    ">> *list高级方法 如：\n",
    ">>> 1.copy( ) 复制 不等于 赋值，复制可以 等价于 切片[:]\n",
    ">>> 2.enu\n",
    ">> * 课本练习\n",
    ">>> 1.list的一些方法实现\n",
    ">>> 2.list的切片实现\n",
    ">>> 3.str =>list 的方法 list(str)\n",
    ">> 4.list =>str 的方法 ''.join(list)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本周练习\n",
    "\n",
    "1.vowels2.py"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "vowels = ['a','e','i','o','u'] # 这是元音（list）\n",
    "word = \"Milliways\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "e\n",
      "i\n",
      "o\n",
      "u\n"
     ]
    }
   ],
   "source": [
    "for i in vowels: #for循环的第二个元素，决定了我们循环的内容\n",
    "    print(i)     #循环的列表，每个元素单独在一行打印\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "i\n",
      "i\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "for letter in word: #letter相当于一个需要用（东西）来赋值的容器，不会影响结果\n",
    "#    print(letter)   #一般情况下，item（内容）被我们缩写简写为 i\n",
    "  if letter in vowels:\n",
    "        print(letter) #先在word里筛选后再次在vowels里筛选"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'word' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-1-749e393292a1>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;31m#append()经常用于新建列表\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[0mfound\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m \u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mfor\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mword\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[1;32mif\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mvowels\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[1;32mif\u001b[0m \u001b[0mletter\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mfound\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;31m#？\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mNameError\u001b[0m: name 'word' is not defined"
     ]
    }
   ],
   "source": [
    " # append()经常用于新建列表\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "found"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据结构列表练习1\n",
    "\n",
    "购物车程序项目：\n",
    "\n",
    "要求：\n",
    "\n",
    "1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "\n",
    "2、让用户输入商品编号进行商品的购买。\n",
    "\n",
    "3、用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。\n",
    "\n",
    "4、用户可以随时退出购买，推出时打印用户已购买的商品和支付宝余额。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入您的支付宝余额88\n"
     ]
    },
    {
     "ename": "TypeError",
     "evalue": "not all arguments converted during string formatting",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-22-9120223631f4>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m     12\u001b[0m \u001b[1;31m#用户输入(余额)：\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     13\u001b[0m \u001b[0maccount_balance\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"请输入您的支付宝余额\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 14\u001b[1;33m info = '''----------goods details----------\n\u001b[0m\u001b[0;32m     15\u001b[0m \u001b[0m编号0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m商品名称\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0m商品的价格\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0ms\u001b[0m \u001b[0m元\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     16\u001b[0m \u001b[0m编号1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m商品名称\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0m商品的价格\u001b[0m\u001b[1;33m:\u001b[0m \u001b[1;33m%\u001b[0m\u001b[0ms\u001b[0m \u001b[0m元\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mTypeError\u001b[0m: not all arguments converted during string formatting"
     ]
    }
   ],
   "source": [
    "# 1.商品信息\n",
    "\n",
    "# 这是小蔡写的，很复杂\n",
    "# goods_lists = [\"apple\",\"orange\",\"pear\"] \n",
    "# goods_number_lists = [\"111\",\"222\",\"333\"] \n",
    "\n",
    "goods_lists = [\"apple\",5],[\"orange\",10],[\"pear\",3] #老师写的，用了嵌套，把名称和价格嵌套起来 \n",
    "\n",
    "# 2.用户购物信息\n",
    "user_shopping_lists = [\"apple\",\"\",\"\",\"\"]\n",
    "\n",
    "#用户输入(余额)：\n",
    "account_balance = input(\"请输入您的支付宝余额\")\n",
    "info = '''----------goods details----------\n",
    "编号0, 商品名称: %s,商品的价格: %s 元\n",
    "编号1, 商品名称: %s,商品的价格: %s 元\n",
    "编号2, 商品名称: %s,商品的价格: %s 元\n",
    "'''%(goods_lists[0],[0],goods_lists[0],[1],\n",
    "    goods_lists[1],[0],goods_lists[1],[1],\n",
    "    goods_lists[2],[0],goods_lists[2],[1],) \n",
    "\n",
    "print(info)  #取数从0开始，用了占位符，回去搜\n",
    "\n",
    "While True:\n",
    "    print(\"------goods_list------\")\n",
    "    # for循环的优势，减少重复代码的书写 \n",
    "for i in range(4):\n",
    "   \n",
    "    print(\"编号%s,商品名称:%s,商品价格:%s元,\"\n",
    "         %(i,goods_list[i][0],goods_list[i][1]))\n",
    "    \n",
    "    ### 用户输入商品编号\n",
    "    choice = int(input(\"请输入购买商品编号:\"))\n",
    "    ### 1.输入编号存在，判断余额是否足够\n",
    "    if choice < len(goods_list):        \n",
    "                ### 1.余额够，用户余额-商品价格\n",
    "        if goods_list[choice][1] < Alipay:\n",
    "            Alipay = Alipay - goods_list[choice][1]\n",
    "            print(\"您的商品购买成功，已添加到购物车，您的余额还有%s\"%(Alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足，请用花呗吧!\")\n",
    "     \n",
    "        ### 2.余额不足，“您的余额不足”\n",
    "        else:\n",
    "            print(\"您输入的商品编号可能超过我们的商品范围或不存在这个商品\")\n",
    "            \n",
    "\n",
    "#1. 输入正确，打印商品列表\n",
    "\n",
    "\n",
    "##用户输入商品编号\n",
    "## 1.输入的编号存在，判断余顿是否够\n",
    "\n",
    "\n",
    "### 1.余额够，用户余额一商品价格\n",
    "##  2.余额不够，“您的余领不足\n",
    "##  2.输入的编号不存在， 请输入正确的余额”\n",
    "#   2. 输入错误， 请输入正确的余额”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "goods_list =[\n",
    "            [\"skirt\",39],\n",
    "            [\"shoes\",100],\n",
    "            [\"clothes\",60],\n",
    "            [\"drink\",10]\n",
    "]\n",
    "\n",
    "info = '''------goods detail------\n",
    "编号0，商品名称：%s,商品价格：%s 元\n",
    "编号1，商品名称：%s,商品价格：%s 元\n",
    "编号2，商品名称：%s,商品价格：%s 元\n",
    "编号3，商品名称：%s,商品价格：%s 元\n",
    "'''%(goods_list[0][0],goods_list[0][1],\n",
    "    goods_list[1][0],goods_list[1][1],\n",
    "    goods_list[2][0],goods_list[2][1],\n",
    "    goods_list[3][0],goods_list[3][1],) # [0]为index内容的位置\n",
    "print(info)\n",
    "\n",
    "while True:\n",
    "    print(\"------goods detail------\")\n",
    "    break\n",
    "\n",
    "# for循环的优势，节省重复代码的书写\n",
    "for i in range(4): #i 是0，1，2，3\n",
    "    print(\"编号%s，商品名称%s: %s元\" %(i,goods_list[i][0],goods_list[i][1]))\n",
    "\n",
    "# 用户输入商品编号\n",
    "\n",
    "user_shopping_list = []\n",
    "\n",
    "count = 3\n",
    "while True:\n",
    "    banlance = 100\n",
    "    Alipy = int(input(\"请输入您的支付宝余额:\"))\n",
    "    count -=1\n",
    "\n",
    "    if Alipy == banlance:\n",
    "        print(\"您输入的支付宝余额正确！\")\n",
    "        choice = int(input(\"请输入购买的商品编号：\"))\n",
    "   # 输入编号存在，判断余额是否够\n",
    "        if choice < len(goods_list):\n",
    "       #余额够——商品余额-商品价格\n",
    "            if goods_list[choice][1] <=Alipy:\n",
    "                Alipy = Alipy - goods_list[choice][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还剩%s\" % (Alipy))\n",
    "                print(\"购买清单\\n编号%s,商品名称%s：%s元\"%(choice,goods_list[choice][0],goods_list[choice][1]))\n",
    "\n",
    "           # 不够——\"你的余额不足\"\n",
    "            else:\n",
    "                print(\"不好意思！您的余额不足！\")\n",
    "\n",
    "    else:\n",
    "       # 输入编号不存在，输入正确的余额\n",
    "        print(\"您输入的编号不存在，请重新输入！\")\n",
    "        choice_2 = int(input(\"重新输入购买商品的编号：\"))\n",
    "        if choice_2 < len(goods_list):\n",
    "### 1. 余额够，用户余额 - 商品价格\n",
    "            if goods_list[choice_2][1] <= Alipy:\n",
    "                Alipy = Alipy - goods_list[choice_2][1]\n",
    "                print(\"您的商品购买成功，已添加到购物车，您的余额还剩%s\" %(Alipy))\n",
    "                print(\"购买清单\\n编号%s，商品名称%s：%s元\" % (choice_2,goods_list[choice_2][0],goods_list[choice_2][1]))\n",
    "                break\n",
    "### 2. 余额不够，“您的余额不足”\n",
    "            else:\n",
    "                print(\"不好意思，您的余额不足！无法购买\")\n",
    "            break\n",
    "\n",
    "else:\n",
    "    if count == 0:\n",
    "        print(\"您的账号无法使用！\")\n",
    "\n",
    "    else:\n",
    "        print(\"输入错误，您还有%d次机会！\" % count)\n",
    "print(\"over!\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
